package serviceImp;

import java.util.List;

import org.apache.log4j.Logger;
import org.hamcrest.SelfDescribing;

import dao.StoptimeHome;
import dao.UserHome;
import dao.VideoHome;

import pojo.Stoptime;
import pojo.User;
import pojo.Video;
import service.IMemberShipService;

/**
 * @author Gang
 *
 */
public class MembershipServiceImp implements IMemberShipService {
    

    private static final Logger _logger = Logger
	    .getLogger(MembershipServiceImp.class);
    private UserHome userDAO;
    
    private StoptimeHome stoptimeDAO;
    
    public void setUserDAO(UserHome userDAO) {
        this.userDAO = userDAO;
    }

    public void setStoptimeDAO(StoptimeHome stoptimeDAO) {
        this.stoptimeDAO = stoptimeDAO;
    }


    public MembershipServiceImp(){
	
	
    }
    
    

    public User login(String user, String pass) {
	User exampleUser = new User();
	exampleUser.setUsername(user);
	exampleUser.setPassword(pass);

	List<User> findUser = userDAO.findByExample(exampleUser);
	
	if (findUser.size()>0) {
	    return findUser.get(0);	    
	}else {
	    return null;
	}
	
    }

    public User register(User user) {
	User newUser = user;
	
	try {
	    
	    if (isUsernameExist(user.getUsername())) {
		return null;
	    }
	    
	    userDAO.attachDirty(newUser);
	    return (User) userDAO.findByExample(newUser).get(0);
	} catch (RuntimeException e) {
	    _logger.log(org.apache.log4j.Level.ERROR, "register new user fail");
	    return null;
	}
	
	
	
    }

    public void logout(int userID) {
	// TODO Auto-generated method stub
	// Do nothing now
    }

    public User updateUser(User user) {
	User newUser = user;
	
	try {
	    userDAO.attachDirty(newUser);
	    return (User) userDAO.findByExample(newUser).get(0);
	} catch (RuntimeException e) {
	    _logger.log(org.apache.log4j.Level.ERROR, "update user fail");
	    return null;
	}
    }



    public boolean isUsernameExist(String username) {
	User exampleUser = new User();
	exampleUser.setUsername(username);


	List<User> findUser = userDAO.findByExample(exampleUser);
	
	if (findUser.size()>0) {
	    return true;	    
	}else {
	    return false;
	}
    }

    public Video lastWatchVideo(User user) {
	
	Stoptime example = new Stoptime();
	
	example.setUser(user);
	
	List<Stoptime> list = stoptimeDAO.findByExample(example);
	
	if (list.size()>0) {
	    return list.get(0).getVideo();
	}
	
	return null;
    }

    public void saveLastWatch(User user, Video video, int minutes) {
	
	Stoptime example = new Stoptime();
	
	example.setUser(user);
	
	List<Stoptime> list = stoptimeDAO.findByExample(example);
	
	
	//if user has laststop record
	if (list.size() > 0 ) {
	    
	    Stoptime lastwatch = list.get(0);
	    
	    lastwatch.setVideo(video);
	    lastwatch.setStopMinutes(minutes);
	       
	    stoptimeDAO.attachDirty(lastwatch);
   
	}else{
	    Stoptime lastwatch = new Stoptime();
	    
	    lastwatch.setUser(user);
	    lastwatch.setVideo(video);
	    lastwatch.setStopMinutes(minutes);
	    
	    stoptimeDAO.attachDirty(lastwatch);
	    
	    
	}
		
    }

}
